/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package cn.edu.thu.log.web.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import storage.dbhelper.BusinessPatternHelper;
import storage.dbhelper.VersionHelper;
import storage.dbobject.DbBusinessPattern;
import storage.dbobject.DbVersion;
import cn.edu.thu.log.preprocessrule.OneToMoreUtil;
import cn.edu.thu.log.web.service.WebConfigReadService;
/**
 * 
 * @author dongzihe
 *
 */
public final class WebConfigReadServiceImpl implements WebConfigReadService {

	private Map<String, String> logCleanList;
	private Map<String, OneToMoreUtil> noisestringList;
	private String min = null;
	private String max = null;
	private ArrayList<String> activityIDList;
	private String timestamp = null;
	private ArrayList<String> productList;
	private ArrayList<String> caseIDList;
	private OneToMoreUtil temp;
	private String patternName;
	private String versionName;
	private DbVersion version;
	private DbBusinessPattern pattern;
	private String classifyTags;
	// private String miningconfigfilename=null;
	private static WebConfigReadService singleton = null;
/**
 * 
 * @return
 */
	public static synchronized WebConfigReadService instance() {
		if (singleton == null) {
			singleton = new WebConfigReadServiceImpl();
		}
		return singleton;
	}

	private WebConfigReadServiceImpl() {
		activityIDList = new ArrayList<String>();
		caseIDList = new ArrayList<String>();
		productList = new ArrayList<String>();
		logCleanList = new HashMap<String, String>();
		noisestringList = new HashMap<String, OneToMoreUtil>();
		temp = new OneToMoreUtil();
		patternName = "";
		versionName = "";
		classifyTags = "";
	}

	@Override
	public void config() {
		VersionHelper versionH = new VersionHelper();
		List<DbVersion> versions = versionH.getVersions(
				DbVersion.CL_VERSIONNUM, versionName);
		setVersion(versions.get(0));

		BusinessPatternHelper businessPatternH = new BusinessPatternHelper();
		List<DbBusinessPattern> patterns = businessPatternH
				.getBusinessPatterns(DbBusinessPattern.CL_NAME, patternName);
		setPattern(patterns.get(0));
	}

	@Override
	public void readWebConfig(String filename) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		// 解析文档
		Document doc = null;
		try {
			doc = builder.parse(new File(filename));

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 获得根节点miningconfig
		Element root = doc.getDocumentElement();
		Element activityidentify = (Element) root.getElementsByTagName(
				"activityidentify").item(0);
		Element caseidentfy = (Element) root.getElementsByTagName(
				"caseidentify").item(0);
		NodeList activityruleList = activityidentify
				.getElementsByTagName("activity");
		NodeList timestampList = activityidentify
				.getElementsByTagName("timestamp");
		NodeList proList = activityidentify.getElementsByTagName("product");
		NodeList caseruleList = caseidentfy.getElementsByTagName("case");


		for (int i = 0; i < activityruleList.getLength(); i++) {
			Element temp1 = (Element) activityruleList.item(i);
			activityIDList.add(temp1.getAttribute("activityname"));
		}

		Element time = (Element) timestampList.item(0);
		timestamp = new String(time.getAttribute("timestamp"));

		for (int i = 0; i < proList.getLength(); i++) {
			Element products = (Element) proList.item(i);
			productList.add(products.getAttribute("productname"));
		}

		for (int i = 0; i < caseruleList.getLength(); i++) {
			Element cases = (Element) caseruleList.item(i);
			caseIDList.add(cases.getAttribute("caseid"));
		}
	}

	@Override
	public ArrayList<String> getCaseIDList() {
		return caseIDList;
	}

	@Override
	public ArrayList<String> getActivityIDList() {
		return activityIDList;
	}

	@Override
	public String getTimeStamp() {
		return timestamp;
	}

	@Override
	public Map<String, String> getFormatActivityIDList() {
		return null;
	}

	@Override
	public ArrayList<String> getProductsList() {
		return null;
	}

	@Override
	public Map<String, String> getLogCleanList() {
		return logCleanList;
	}

	@Override
	public Map<String, OneToMoreUtil> getNoiseStringList() {
		return noisestringList;
	}

	@Override
	public String getMin() {
		return min;
	}

	@Override
	public String getMax() {
		return max;
	}

	@Override
	/**
	 * @return the patternName
	 */
	public String getPatternName() {
		return patternName;
	}

	@Override
	/**
	 * @param patternName the patternName to set
	 */
	public void setPatternName(String patternName) {
		this.patternName = patternName;
	}

	@Override
	/**
	 * @return the versionName
	 */
	public String getVersionName() {
		return versionName;
	}

	@Override
	/**
	 * @param versionName the versionName to set
	 */
	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	@Override
	/**
	 * @return the version
	 */
	public DbVersion getVersion() {
		return version;
	}

	@Override
	/**
	 * @param version the version to set
	 */
	public void setVersion(DbVersion version) {
		this.version = version;
	}

	@Override
	/**
	 * @return the pattern
	 */
	public DbBusinessPattern getPattern() {
		return pattern;
	}

	@Override
	/**
	 * @param pattern the pattern to set
	 */
	public void setPattern(DbBusinessPattern pattern) {
		this.pattern = pattern;
	}

	@Override
	public String getClassifyTagList() {
		return classifyTags;
	}

}
